package de.zainodis.triplog.ui.fragment;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import de.zainodis.triplog.R;
import de.zainodis.triplog.data.DataProvider;
import de.zainodis.triplog.data.DiaryEntry;
import de.zainodis.triplog.ui.task.IAsyncTaskParent;
import de.zainodis.triplog.ui.task.NoteLoader;

/**
 * Lets the user flip through recorded notes.
 * Created by fzarrai on 10/4/2014.
 */
public class NoteFlipperFragment extends Fragment {

    private Flipper adapter;
    private ViewPager pager;

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_note_flipper, container, false);
        return root;
    }

    public void onUpdate(Context context) {
        //Load Notes
        new NoteLoader(context, OnNotesLoaded).execute(DataProvider.getCurrentTrip().getId());
    }

    private IAsyncTaskParent OnNotesLoaded = new IAsyncTaskParent<List<DiaryEntry>>() {

        @Override
        public void onPostExecute(List<DiaryEntry> result) {
            if (getActivity() == null || getActivity().isFinishing()) {
                return;
            }
            //Update result
            DataProvider.updateNotes(result);

            List<DiaryEntry> notes = new ArrayList<DiaryEntry>(DataProvider.getNotes());
            if (notes.size() == 0) {
                //Show default note
                DiaryEntry entry = new DiaryEntry();
                entry.setText(getString(R.string.no_notes_available));
                notes = new ArrayList<DiaryEntry>();
                notes.add(entry);
            }
            //Update the adapter and currently showing item
            adapter.update(notes);
            pager.setCurrentItem(notes.size() - 1);
        }
    };

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //TODO show wait spinner until notes are loaded
        pager = (ViewPager) view.findViewById(R.id.fragment_note_flipper_pager);
        adapter = new Flipper(getActivity().getLayoutInflater());
        pager.setAdapter(adapter);
    }

    private class Flipper extends PagerAdapter {

        private final LayoutInflater inflater;
        private List<DiaryEntry> notes = new ArrayList<DiaryEntry>();

        public Flipper(LayoutInflater inflater) {
            this.inflater = inflater;
        }

        public void update(List<DiaryEntry> notes) {
            this.notes = notes;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return notes.size();
        }

        public Object instantiateItem(ViewGroup collection, int position) {
            //TODO consider adding delete on long press
            ScrollView scroll = (ScrollView) inflater.inflate(R.layout.widget_scrollview, collection, false);
            TextView view = (TextView) inflater.inflate(R.layout.widget_textview, scroll, false);
            view.setText(notes.get(position).getText());
            scroll.addView(view);
            collection.addView(scroll);
            return scroll;
        }

        @Override
        public int getItemPosition(Object object) {
            /*We require this so our view is always updated properly. It generally works without this snippet,
            unless the first page changes (i.e. number of notes does not change, just the content of the note) - in that case the view is not updated properly.*/
            return POSITION_NONE;
        }

        @Override
        public void destroyItem(ViewGroup collection, int position,
                                Object view) {
            ((ViewPager) collection).removeView((ScrollView) view);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((ScrollView) object);
        }

    }

}
